package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.enums.RedisConstants;
import com.weishi.framework.redis.RedisCache;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.training.domain.ShipSpeed;
import com.weishi.project.training.mapper.ShipSpeedMapper;
import com.weishi.project.training.service.IShipSpeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 船速仪Service业务层处理
 */
@Service
public class ShipSpeedServiceImpl extends ServiceImpl<ShipSpeedMapper, ShipSpeed> implements IShipSpeedService {

    @Autowired
    private ShipSpeedMapper shipSpeedMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询船速仪列表
     */
    @Override
    public List<ShipSpeed> selectShipSpeedList(ShipSpeed shipSpeed) {

        List<ShipSpeed> list = shipSpeedMapper.selectShipSpeedList(shipSpeed);
        for(ShipSpeed shipSpeed1 : list){
            Integer status = redisCache.getCacheObject(RedisConstants.ShipSpeed_Status_Prefix + shipSpeed1.getSn());
            if(status!=null && status==1){
                shipSpeed1.setStauts(1);
            }
        }
        return list;
    }

    @Override
    public int getShipSpeedStatus(String ShipSpeedNo) {
        Integer status = redisCache.getCacheObject(RedisConstants.ShipSpeed_Status_Prefix + ShipSpeedNo);
        if(status!=null && status==1){
            return 1;
        }
        return  0;
    }

    @Override
    public boolean save(ShipSpeed entity) {
        boolean ret = super.save(entity);
        //加到缓存数据
        redisCache.setCacheObject(RedisConstants.ShipSpeed_Info_Prefix + entity.getSn(),entity);
        return ret;
    }

    @Override
    public boolean removeByIds(List<Long> idList) {
        for(Long id : idList){
            ShipSpeed shipSpeed = super.getById(id);
            String sn = shipSpeed.getSn();
            super.removeById(id);
            redisCache.deleteObject(RedisConstants.ShipSpeed_Info_Prefix + sn);
        }
        return true;
    }


    @Override
    public boolean updateById(ShipSpeed entity) {
        boolean ret = super.updateById(entity);
        redisCache.setCacheObject(RedisConstants.ShipSpeed_Info_Prefix + entity.getSn(),entity);
        return ret;
    }



    /**
     * 教练员端 -- 查询所有船速仪列表
     */
    @Override
    public AjaxResult getShipSpeed(ShipSpeed shipSpeed) {
        return AjaxResult.success(list());
    }







}